﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;

namespace MMS
{
    public partial class FormMain : Form
    {
        private static readonly ILog log =
            LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        string selectedLink = "";
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //UserControlRentReturnList fcustomerList = new UserControlRentReturnList() { /*fcustomerList.TopLevel = false;*/Visible = true };
            //splitContainerControl1.Panel2.Controls.Add(fcustomerList);

        }

        private void navBarControl1_SelectedLinkChanged(object sender, DevExpress.XtraNavBar.ViewInfo.NavBarSelectedLinkChangedEventArgs e)
        {
            loadRightPanelOnSelectedLink(e.Link.ItemName);
        }

        private void loadRightPanelOnSelectedLink(string itemName)
        {
            UserControl uc = null;
            Form form = null;

            if (selectedLink == itemName)
            {
                return;
            }

            //danh muc khach hang
            if (itemName == navItemCustomerList.Name)
            {
                uc = new UserControlCustomerList();
            }
            //them phieu nhap hang
            else if (itemName == navItemImport.Name)
            {

            }
            //danh sach phieu nhap hang
            else if (itemName == navItemImportedList.Name)
            {
                
            }
            //them phieu nhap hang
            else if (itemName == navItemUserList.Name)
            {
                uc = new UserControlUserList();
            }
            //danh sach kiem ke kho
            else if (itemName == navItemInventoryList.Name)
            {

            }
            //danh muc vat tu
            else if (itemName == navItemMaterialList.Name)
            {
                uc = new UserControlMaterialList();
            }
            //them phieu muon vat tu
            else if (itemName == navItemOrder.Name)
            {
                form = new FormRentMaterial();
            }
            //danh sach muon vt
            else if (itemName == navItemRentList.Name)
            {
                uc = new UserControlRentList();
            }          
            //bang tinh tien thue
            else if (itemName == navItemRentsSheet.Name)
            { }
            //thong ke hang trong kho
            else if (itemName == navItemRepositoryStatistic.Name)
            { }
            //them phieu muon tam
            //else if (itemName == navItemTempOrder.Name)
            //{ 
            //}
            //danh muc don vi tinh
            else if (itemName == navItemUnitList.Name)
            {
                uc = new UserControlUnitList();
            }
            //danh muc nha cung cap
            else if (itemName == navItemVendorList.Name)
            {
                uc = new UserControlVendorList();
            }


            if (uc != null)
            {
                splitContainerControl1.Panel2.Controls.Clear();
                splitContainerControl1.Panel2.Controls.Add(uc);
                selectedLink = itemName;
            }
            if (form != null)
            {
                form.ShowDialog(this);
            }
        }

        private void setLoggedInInterface()
        {
            navBarControl1.Enabled = true;
        }

        private void setLoggedOutInterface()
        {
            navBarControl1.Enabled = false;
        }

        private void doLogin()
        {
            try
            {
                FormLogin formLogin = new FormLogin();
                formLogin.ShowDialog();
                if (formLogin.IsConnected)
                {
                    setLoggedInInterface();
                }
            }
            catch (Exception e)
            {
                log.Error(e);
            }
        }
        private void navBarControl1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            loadRightPanelOnSelectedLink(e.Link.ItemName);
        }

        private void FormMain_Shown(object sender, EventArgs e)
        {
            setLoggedOutInterface();
            doLogin();
        }
    }
}
